from django.utils.translation import gettext_lazy as _

from chatbot.component.singleton import singleton


@singleton
class Const(object):
    SUPER_USER_ID = 1
    SUPER_TENANT_ID = 1
    SLOT_TYPE = (
        (0, _('Integer')),
        (1, _('Decimal')),
        (2, _('Text')),
        (3, _('Enum')),
        (4, _('Date')),
        (5, _('DateTime')),
        (6, _('Time')),
        (7, _('TimeInterval')),
        (8, _('URL')),
        (9, _('Price')),
        (10, _('Country')),
        (11, _('City')),
        (12, _('Person')),
        (13, _('Org')),
        (14, _('AirPort')),
        (15, _('Phone')),
        (16, _('Software')),
        (17, _('Unit')),
        (18, _('Brand')),
        (19, _('Length')),
        (20, _('Mass')),
        (21, _('Volume')),
        (22, _('Temperature')),
        (23, _('Area')),
    )
    SLOT_TYPE_INTEGER = 0
    SLOT_TYPE_FLOAT = 1
    SLOT_TYPE_TEXT = 2
    SLOT_TYPE_ENUM = 3
    SLOT_TYPE_DATE = 4
    SLOT_TYPE_DATETIME = 5
    SLOT_TYPE_TIME = 6
    SLOT_TYPE_TIME_INTERVAL = 7
    SLOT_TYPE_URL = 8
    SLOT_TYPE_PRICE = 9
    SLOT_TYPE_COUNTRY = 10
    SLOT_TYPE_CITY = 11
    SLOT_TYPE_PERSON = 12
    SLOT_TYPE_ORG = 13
    SLOT_TYPE_AIRPORT = 14
    SLOT_TYPE_PHONE = 15
    SLOT_TYPE_SOFTWARE = 16
    SLOT_TYPE_UNIT = 17
    SLOT_TYPE_BRAND = 18
    SLOT_TYPE_LENGTH = 19
    SLOT_TYPE_MASS = 20
    SLOT_TYPE_VOLUME = 21
    SLOT_TYPE_TEMPERATURE = 22
    SLOT_TYPE_AREA = 23

    WORD_TYPE = (
        (0, _('Normal Word')),
        (1, _('Banned Word')),
        (2, _('Stop Word')),
    )
    WORD_TYPE_NORMAL_WORD = 0
    WORD_TYPE_BANNED_WORD = 1
    WORD_TYPE_STOP_WORD = 2

    WORD_SOURCE = (
        (0, _('Customer Add')),
        (1, _('Entity')),
        (2, _('Property')),
        (3, _('Value')),
    )
    WORD_SOURCE_CUSTOMER_ADD = 0
    WORD_SOURCE_ENTITY = 1
    WORD_SOURCE_PROPERTY = 2
    WORD_SOURCE_VALUE = 3

    WORD_RELATION_TYPE = (
        (0, _('Synonym')),
        (1, _('Hyponym')),
        (2, _('Similarity')),
    )

    WORD_RELATION_TYPE_SYNONYM = 0
    WORD_RELATION_TYPE_HYPONYM = 1
    WORD_RELATION_TYPE_SIMILARITY = 2

    COMPARISON_PATTERN = {
        'GT': (('比(.+?)贵', 'Price', '贵', '元'),
               ('比(.+?)高', 'Length', '高', 'mm'),
               ('比(.+?)重', 'Mass', '大', 'g'),
               ('比(.+?)大', 'Volume', '大', '平米'),
               ('比(.+?)长', 'Length', '长', 'mm'),
               ('比(.+?)宽', 'Length', '宽', 'mm'),
               ),
        'LT': (('比(.+?)便宜', 'Price', '便宜', '元'),
               ('比(.+?)矮', 'Length', '矮', 'mm'),
               ('比(.+?)轻', 'Mass', '轻', 'g'),
               ('比(.+?)小', 'Volume', '小', '平米'),
               ('比(.+?)短', 'Length', '短', 'mm'),
               ('比(.+?)窄', 'Length', '窄', 'mm'),
               ),
        'AROUND': (('在(.+?)之间', 'Length', '', ''),
                   ('在(.+?)上下', 'Length', '', ''),
                   ('在(.+?)左右', 'Length', '', ''),
                   ),
    }

    STATE = (
        (0, _("Active")),
        (1, _("Deactivated")),
        (2, _("Debugging")),
        (3, _("Training")),
        (4, _('To Be Effective')),
        (5, _('Approval To Delete')),
        (6, _('Approval To Online')),
        (7, _('Approval To Offline')),
    )
    STATE_ACTIVE = 0
    STATE_DEACTIVATED = 1
    STATE_DEBUGGING = 2
    STATE_TRAINING = 3
    STATE_TO_BE_EFFECTIVE = 4

    OPERATION = (
        'LT', 'GT', 'LTE', 'GTE', 'BETWEEN', 'AROUND',
    )
    LESS_THEN = 'LT'
    GREATER_THEN = 'GT'
    LESS_THEN_OR_EQUAL = 'LTE'
    GREATER_THEN_OR_EQUAL = 'GTE'
    AROUND = 'AROUND'

    SUMMARY = 'SUM'
    AVERAGE = 'AVG'
    MAXIMUM = 'MAX'
    MINIMUM = 'MIN'
    COUNT = 'COUNT'
    FIRST = 'FIRST'
    LAST = 'LAST'

    AGGREGATION = (
        'SUM', 'AVG', 'MAX', 'MIN', 'COUNT', 'FIRST', 'LAST',
    )

    SEG_SEPARATOR = '#'

    CHANNEL_TYPE = (
        (0, _('Web')),
        (1, 'App'),
        (2, _('Wechat')),
    )
    CHANNEL_TYPE_WEB = 0
    CHANNEL_TYPE_APP = 1
    CHANNEL_TYPE_WECHAT = 2

    QUESTION_TYPE = (
        (0, _("Text")),
        (1, _("Image")),
        (2, _("Video")),
        (3, _("Audio")),
        (4, _("File")),
        (5, _("Link")),
    )
    QUESTION_TYPE_TEXT = 0
    QUESTION_TYPE_IMAGE = 1
    QUESTION_TYPE_VIDEO = 2
    QUESTION_TYPE_AUDIO = 3
    QUESTION_TYPE_FILE = 4
    QUESTION_TYPE_LINK = 5

    ANSWER_SOURCE = (
        (0, _("Fixed Script")),
        (1, "FAQ"),
        (2, _("Chit")),
        (3, _("Multi-turn")),
        (4, _("Table")),
        (5, _("MRC")),
    )
    ANSWER_SOURCE_FIXED_SCRIPT = 0
    ANSWER_SOURCE_FAQ = 1
    ANSWER_SOURCE_CHIT = 2
    ANSWER_SOURCE_MULTI_TURN = 3
    ANSWER_SOURCE_TABLE = 4
    ANSWER_SOURCE_MRC = 5

    MATCH_TYPE = (
        (0, _("Directly Reply")),
        (1, _("Confirmed Guide Reply")),
        (2, _("Unconfirmed Guide Reply")),
        (3, _("Unknown Reply")),
    )
    MATCH_TYPE_DIRECTLY_REPLY = 0
    MATCH_TYPE_CONFIRMED_GUIDE_REPLY = 1
    MATCH_TYPE_UNCONFIRMED_GUIDE_REPLY = 2
    MATCH_TYPE_UNKNOWN_REPLY = 3

    MATCH_SOURCE_TYPE = (
        (0, _("Standard Question")),
        (1, _("Similar Question")),
        (2, _("Fixed Scene")),
    )
    MATCH_SOURCE_TYPE_STANDARD_QUESTION = 0
    MATCH_SOURCE_TYPE_SIMILAR_QUESTION = 1
    MATCH_SOURCE_TYPE_FIXED_SCENE = 2

    SATISFACTION = (
        (0, _("Not Evaluated")),
        (1, _("Very Good")),
        (2, _("Satisfied")),
        (3, _("Fair")),
        (4, _("Not Satisfied")),
        (5, _("Very Bad")),
    )
    SATISFACTION_NOT_EVALUATED = 0
    SATISFACTION_VERY_GOOD = 1
    SATISFACTION_SATISFIED = 2
    SATISFACTION_FAIR = 3
    SATISFACTION_NOT_SATISFIED = 4
    SATISFACTION_VERY_BAD = 5

    AGENT_TYPE = (
        (0, _("Robot")),
        (1, _("Human In Loop")),
    )

    AGENT_TYPE_ROBOT = 0
    AGENT_TYPE_HUMAN_IN_LOOP = 1

    CONFIGURATION_TYPE = (
        (0, _("Function Switch")),
        (1, _("Value")),
    )

    CONFIGURATION_TYPE_FUNCTION_SWITCH = 0

    SKILL_TYPE = (
        (0, _("Logic Adapter")),
        (1, _("Preprocessor"))
    )
    SKILL_TYPE_LOGIC_ADAPTER = 0

    FILTER_TYPE_ALL = _('All')

    class ConstError(TypeError):
        pass

    class ConstCaseError(ConstError):
        pass

    def __setattr__(self, key, value):
        print()
        if key in self.__dict__.keys():
            # 存在性验证
            raise self.ConstError("Can't change a self variable: '%s'" % key)

        if not key.isupper():
            # 语法规范验证
            raise self.ConstCaseError("self variable must be combined with upper letters:'%s'" % key)

        __dict__[key] = value
